package com.hjf.ditubiaozhu.base.recycler.widget;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;


public class MySwipeRefreshLayout extends SwipeRefreshLayout {

    private boolean isMeasured = false; //标志位 是否测量完成
    private boolean mPreMeasureRefreshing;

    public MySwipeRefreshLayout(Context context) {
        this(context, null);
    }

    public MySwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //如果是首次测量时,标志位 为测量完成 并设置刷新可见,再次调用时候,如果已经被setRefreshing过一次,自动触发刷新
        if (!isMeasured) {
            isMeasured = true;
            setRefreshing(mPreMeasureRefreshing);
        }
    }

    /**
     * 扩展swipeLayout自带的刷新方法,如果已经测量完成,可以刷新,如果没有测量完成时,调用刷新,就不刷新
     * @param refreshing
     */
    @Override
    public void setRefreshing(boolean refreshing) {
        if (isMeasured) {
            super.setRefreshing(refreshing);
        } else {
            mPreMeasureRefreshing = refreshing;
        }
    }
}
